

cnt=0;
all_act=[];

cond=[-2 -1 1 2];
layer=2;

for ind=1:17
    
    if hom_data(ind).layer==layer
        2
        if size(hom_data(ind).raw_data,2)==6
            tind=[2:6];
        else
            tind=[1:5];
        end
        nbr_cells=sum(hom_data(ind).raw_data(1).ROItype==99);
        tcnt=0;
        for knd=tind
            tcnt=tcnt+1;
            cell_ind=hom_data(ind).raw_data(knd).ROItype==99;
            l_ind=zeros(length(hom_data(ind).raw_data(knd).act_code),1);
            for jnd=1:length(cond)
                l_ind=l_ind+double(hom_data(ind).raw_data(knd).act_code==cond(jnd));
            end
            l_ind=logical(l_ind);
            tmp=hom_data(ind).raw_data(knd).sig_act(l_ind,cell_ind);
            tmp=tmp./repmat(median(tmp),size(tmp,1),1);
            
            all_act(cnt+1:cnt+nbr_cells,tcnt)=mean(tmp);
        end
        cnt=cnt+nbr_cells;
    end
end

all_act=all_act-1;

n_groups=4;
sort_id=kmeans(all_act,n_groups);

figure
% 
% subplot(211)
% hold on
% colorind='rgbmc';
% for ind=1:n_groups
%     plot((all_act(sort_id==ind,:))',colorind(ind))
% end
% 
% subplot(212)
hold on
for ind=1:n_groups
    plot(mean(all_act(sort_id==ind,:))',colorind(ind),'linewidth',4)
    disp([colorind(ind) ' ' num2str(sum(sort_id==ind))])
end
plot(median(all_act(~(sort_id==4),:)),'k','linewidth',4)

